package com.icarsaas.saas.common.page;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * jggrid 分页实体
 * @author admin
 *
 * @param <T>
 */
public class IPager<T> {
    private static Logger log = LoggerFactory.getLogger(IPager.class);
    private Integer page;
    private String sord;
    private String sidx;
    private Integer rows;
    private Boolean _search;
    private String filters;
    private Filter filterObj;
    @SuppressWarnings("rawtypes") private Map params = new HashMap();

    public Integer getLimit() {
        return this.rows; // To change body of implemented methods use File | Settings | File Templates.
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public String getSord() {
        return sord;
    }

    public void setSord(String sord) {
        this.sord = sord;
    }

    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }

    @SuppressWarnings("rawtypes")
	public Map getParams() {
        return params;
    }

    @SuppressWarnings("rawtypes")
	public void setParams(Map params) {
        this.params = params;
    }

    public Integer getStart() {
        if ((getLimit() != null) && (getPage() != null)) {
            return (getPage() - 1) * getLimit();
        }
        return 0;
    }

    public String getSidx() {
        return sidx;
    }

    public void setSidx(String sidx) {
        this.sidx = sidx;
    }

    public String getOrderBy() {
        if (StringUtils.isNotEmpty(getSidx())) {
            return getSidx() + " " + getSord();
        }
        return "";
    }

    public Boolean get_search() {
        return _search;
    }

    public void set_search(Boolean _search) {
        this._search = _search;
    }

    public String getFilters() {
        return filters;
    }

    @SuppressWarnings("unchecked")
	public void setFilters(String filters) {
        if (StringUtils.isNotEmpty(filters)) {
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                filters = filters.replaceAll("&amp;", "&").replaceAll("&lt;", "<").replaceAll("&gt;", ">")
                        .replaceAll("&apos;", "\'").replaceAll("&quot;", "\"").replaceAll("&nbsp;", " ")
                        .replaceAll("&copy;", "@").replaceAll("&reg;", "?");
                Filter filter = objectMapper.readValue(filters, Filter.class);
                if (filter.getRules().length > 0) {
                    for (Rule rule : filter.getRules()) {
                        params.put(rule.getField(), rule.getData());
                    }
                }
                setFilterObj(filter);
            } catch (IOException e) {
                log.error("过滤器初始化失败：{}", e.toString());
                this.filters = null;
            }
        }
        this.filters = filters;
    }

    public Filter getFilterObj() {
        return filterObj;
    }

    public void setFilterObj(Filter filterObj) {
        this.filterObj = filterObj;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
	public Map pageResult(Page page) {
        Map map = new HashMap();
        map.put("page", page.getPageNo());
        if (this.getRows() > 0) {
            map.put("total", Math.ceil((Long.valueOf(page.getCount()).doubleValue() / this.getRows().doubleValue())));
        }
        map.put("rows", page.getList());
        map.put("records", page.getCount());
        return map;
    }

    public Page<T> pageParams(HttpServletRequest request, HttpServletResponse response) {
        Page<T> page = new Page<T>(request, response);
        page.setPageNo(this.getPage());
        page.setPageSize(this.getRows());
        page.setOrderBy(this.getOrderBy());
        return page;
    }
}
